package com.example.shoppingcenter.Controller;

import com.example.shoppingcenter.POJO.Result;
import com.example.shoppingcenter.POJO.User;
import com.example.shoppingcenter.Service.userService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

@Controller
@RequestMapping("/user")
public class userController {
    @Resource
    private userService userService;

    @PostMapping("/login")
    @ResponseBody
    public ResponseEntity<Result> signOn(HttpServletRequest httpServletRequest, String account, String password) {
        Result result = userService.signOn(account, password);
        if(result.isResult()) {
            HttpSession session = httpServletRequest.getSession();
            session.setAttribute("user",account);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/register")
    @ResponseBody
    public ResponseEntity<Result> signUp(HttpServletRequest httpServletRequest, String account, String password, String repassword, String name) {
        Result result = userService.signUp(account, password, repassword,name);
        if(result.isResult()) {
            HttpSession session = httpServletRequest.getSession();
            session.setAttribute("user",account);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/update")
    @ResponseBody
    public ResponseEntity<Result> updateInfo(HttpServletRequest request,@RequestBody User user) {
        User origin = userService.getUserByAccount((String) request.getSession().getAttribute("user"));
        // 获取 User 类中的所有属性
        Field[] fields = User.class.getDeclaredFields();
        for (Field field : fields) {
            try {
                // 获取属性名
                String fieldName = field.getName();
                // 根据属性名构建 getter 方法名
                String getterMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                // 获取 getter 方法
                Method getterMethod = User.class.getMethod(getterMethodName);
                // 调用 getter 方法获取属性值
                Object value = getterMethod.invoke(user);
                // 如果属性值不为 null，则设置到 origin 对象中
                if (value != null) {
                    // 根据属性名构建 setter 方法名
                    String setterMethodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                    // 获取 setter 方法
                    Method setterMethod = User.class.getMethod(setterMethodName, field.getType());
                    // 调用 setter 方法设置属性值到 origin 对象中
                    setterMethod.invoke(origin, value);
                }
            } catch (Exception e) {
                // 处理异常
                e.printStackTrace();
            }
        }
        Result result = userService.updateUser(user);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/changepwd")
    public ResponseEntity<Result> changePwd(HttpServletRequest request,String password) {
        User origin = userService.getUserByAccount((String) request.getSession().getAttribute("user"));
        Result result = userService.changePwd(password, origin.getId());
        if(result.isResult()) {
            request.getSession().removeAttribute("user");
        }
        return ResponseEntity.ok(result);
    }
}
